<?php


namespace app\common\controller;

/**
 * 微信消息推送
 * @package app\common\controller
 */
class News
{
    function Http($url,$data,$type="http"){
        $curl = curl_init();

        if ($type == "json"){
            $headers = array("Content-type: application/json;charset=UTF-8");

            $data=json_encode($data);

            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        }

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);

            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);

        }

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $output = curl_exec($curl);

        curl_close($curl);

        return $output;

    }
    /**
     * 生成url
     */
    public function xcxurl($id,$orderid){
//        $id=$data['id'];
//        $orderid=$data['orderid'];
        $ACCESS_TOKEN = $this->getAccessToken();
        $qcode ="https://api.weixin.qq.com/wxa/generate_urllink?access_token=$ACCESS_TOKEN";
        $param = json_encode(array("path"=>"pages/pagesC/Shopping","query"=>"shopid=$id&orderid=$orderid"));//,"query"=>"id=628","env_version"=>"trial"
        $result = $this->httpRequest( $qcode, $param,"POST");
        $arr = json_decode($result,1);
       // json_decode($result,1);
       // print_r($arr);die;
        return $arr;
    }
    //把请求发送到微信服务器换取二维码
    function httpRequest($url, $data='', $method='GET'){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
        if($method=='POST'){
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data != ''){
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
        }

        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
    }
    public function getAccessToken()
    {
//        $appId = 'wx00e9e5b843259a9d';
//        $secretId ='5a47f8a3b97773614c77da7900a896a3';
        $accessUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET';
        // $url = sprintf($accessUrl,$appId,$secretId);
        $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx00e9e5b843259a9d&secret=5a47f8a3b97773614c77da7900a896a3';
        //print_r($url);die;
        $result = $this->https_request($url);
        $Result = json_decode($result, true);
        return $Result['access_token'];
        // return $Result;

    }
//    public function sendmessage(){
//        $data=$_POST=json_decode(file_get_contents('php://input'), TRUE);
//        $access_token=$this->getAccessToken();
//        $request_url='https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;
//        $request_data=array(
//            'touser'=>"obdeX4v1JCIJdH45QhyJNZnHusBs",//接收者（用户）的 openid
//            'template_id'=>"7pv8mBa8aGl2UTfpxe5UPYpv8OI0lI3nX-kHw2l_R5Y",//所需下发的模板消息的id
//            //'page'=>$data['page'],//点击模板卡片后的跳转页面
//            //'form_id'=>$data['form_id'],//表单提交场景下，为 submit 事件带上的 formId；支付场景下，为本次支付的 prepay_id
//            'data'=>"\"keyword1\": {\"value\": \"339208499\", \"color\": \"#173177\"}",//"keyword1": {"value": "339208499", "color": "#173177"}
//            //'emphasis_keyword'=>$data['emphasis_keyword']//模板需要放大的关键词，不填则默认无放大
//        );
//        $return=json_decode($this->Http($request_url,$request_data,'json'),true);
//        return $return;
//    }
    #curl请求
    function https_request($url,$data = null){
        if(function_exists('curl_init')){
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1);
            if (!empty($data)){
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($curl);
            curl_close($curl);
            return $output;
        }else{
            return false;
        }
    }
    /**
     * @param $data
     * vr通知
     */
    public function vr($data)
    {
        $arr = [
            "touser"=>$data['openid'],
            "template_id"=>"qFe_Sxyot4g5R2qJhpo5ECIp6TvRjYFY3g-WIVAgjXU",
            "page"=>"pages/Home/Home",
            //"form_id"=>$data['formid'],
            "data"=>[
                "thing2"=>[
                    "value"=>$data['thing2'],//产品名称
                ],"thing6"=>[
                    "value"=>$data['thing6'],//产品名称
                ],
            ]
        ];
        $ast = $this->getAccessToken(); //ACCESS_TOKEN
        $rdata = $this->http("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$ast, $arr,"json");
        $rdata = json_decode($rdata,true);
        return $rdata;
    }
    /**
     * @param $data
     * 设计师申请通过通知
     */
    public function desnewss($data){
        $arr = [
            "touser"=>$data['openid'],
            "template_id"=>"7pv8mBa8aGl2UTfpxe5UPYpv8OI0lI3nX-kHw2l_R5Y",
            "page"=>"pages/Home/Home",
            //"form_id"=>$data['formid'],
            "data"=>[
                "thing1"=>[
                    "value"=>$data['thing1'],//产品名称
                ],"thing2"=>[
                    "value"=>$data['thing2'],//产品名称
                ],"date3"=>[
                    "value"=>$data['date3']//申请时间
                ],
            ]
        ];
        $ast = $this->getAccessToken(); //ACCESS_TOKEN
        $rdata = $this->http("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$ast, $arr,"json");
        $rdata = json_decode($rdata,true);
        return $rdata;
    }
    /**
     * @param $data
     * 设计师申请通过通知
     */
    public function desnews($data){

        $arr = [
            "touser"=>$data['openid'],
            //"template_id"=>"7pv8mBa8aGl2UTfpxe5UPYpv8OI0lI3nX-kHw2l_R5Y",
            "template_id"=>"gJOe99DzrAoxLlotExdkNH56NuEr3_3MyMhtKywE83c",
            //7pv8mBa8aGl2UTfpxe5UPYpv8OI0lI3nX-kHw2l_R5Y
            "page"=>"pages/pagesD/designPage/designPage",
            //"form_id"=>$data['formid'],
//            "data"=>[
//                "thing1"=>[
//                    "value"=>$data['thing1'],//产品名称
//                ],"thing2"=>[
//                    "value"=>$data['thing2'],//产品名称
//                ],"date3"=>[
//                    "value"=>$data['time']//申请时间
//                ],
//            ]
             "data"=>[
                "thing1"=>[
                    "value"=>$data['thing1'],//产品名称
                ],"phrase2"=>[
                    "value"=>$data['thing2'],//产品名称
                ],"date3"=>[
                    "value"=>$data['time']//申请时间
                ],"thing5"=>[
                     "value"=>$data['thing5']//申请时间
                 ],

            ]
        ];
        $ast = $this->getAccessToken(); //ACCESS_TOKEN
        $rdata = $this->http("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$ast, $arr,"json");
        $rdata = json_decode($rdata,true);
        return $rdata;
    }
    /**
     * 留言更新通知
     */
    public function liuyan($data){
        $arr = [
            "touser"=>$data['openid'],
            "template_id"=>"auLnrnvAYh0neKlgtVQ5OEDvbppe0KEF8lXVVC0tLZU",
            //"form_id"=>$data['formid'],
//            "data"=>[
//                "keyword1"=>[
//                    "thing3"=>$data['one'],//场景名称
//                    "time2"=>$data['two'],//时间
//                    "thing4"=>$data['three']//留言内容
//                ]
//            ]
            "data"=>[
            "thing3"=>[
                "value"=>$data['thing1'],//产品名称
            ],"time2"=>[
                "value"=>$data['time'],//产品名称
            ],"thing4"=>[
                "value"=>$data['thing4']//申请时间
            ]

        ]
        ];
        $ast = $this->getAccessToken(); //ACCESS_TOKEN
        $rdata = $this->http("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$ast, $arr,"json");
        $rdata = json_decode($rdata,true);
        return $rdata;
    }
    /**
     * 设计师有新的派单提醒
     */
    public function neworder($data){
        $arr = [
            "touser"=>$data['openid'],
            "template_id"=>"P9sDe92NgZMcD2lKBvrLniyriLRhaxIyOGF8tH5El8o",
            //"form_id"=>$data['formid'],
//            "data"=>[
//                "keyword1"=>[
//                    "thing1"=>$data['one'],//订单详情
//                    "phrase2"=>$data['two'],//订单状态
//                    "time8"=>$data['three']//订单时间
//                ]
//            ]
              "data"=>[
            "thing1"=>[
                "value"=>$data['thing1'],//订单详情
            ],"time8"=>[
                "value"=>$data['time'],//订单时间
            ],"phrase2"=>[
                "value"=>$data['thing4']//订单状态
            ]
                  ]
        ];
        $ast = $this->getAccessToken(); //ACCESS_TOKEN
        $rdata = $this->http("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$ast, $arr,"json");
        $rdata = json_decode($rdata,true);
        return $rdata;
    }
    /**
     * 提现到账通知
     */
    public function tixian($data){
        $arr = [
            "touser"=>$data['openid'],
            "template_id"=>"ag6I4iIgY1yo9QDaLolhH-D1e7Rpl_Tszw1SqYZzBDA",
            "form_id"=>$data['formid'],
            "data"=>[
                "keyword1"=>[
                    "number2"=>$data['one'],//银行账号
                    "thing1"=>$data['two'],//银行名称
                    "amount3"=>$data['three'],//提现金额
                    "date5"=>$data['four']//提现时间
                ]
            ]
        ];
        //https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send
        $ast = $this->getAccessToken(); //ACCESS_TOKEN
        $rdata = $this->http("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$ast, $data,"json");
        $rdata = json_decode($rdata,true);
        return $rdata;
    }
}